import requests
import json
from PyCmpltrtok.common import md5, sep


def text_gen(xinput, username='', dport=7750):
    xjson = dict()
    xjson['input'] = xinput
    xjson['username'] = username

    url = f'http://127.0.0.1:{dport}/stream_chat'
    print(url)
    res = requests.post(url, json=xjson, stream=True)
    
    for chunk in res.iter_lines(decode_unicode=False, delimiter=b'\0'):
        if not chunk:
            continue
        chunk = chunk.decode('utf8')
        data = json.loads(chunk)
        xouput = data['output']
        yield xouput


if '__main__' == __name__:
    user = 'u0001'
    gen = text_gen('怎么滑雪？', username=user)
    for x in gen:
        print(x)
        